package com.share.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/*
零、springMvc请求过程：
    request到达DispatcherServlet--->通过HandleMapping获取HandlerExecutionChain（HandlerController
+HandlerInterceptor）--->通过HandlerAdapter处理HandlerController返回ModelAndView--->ViewResolver处理ModelAndView返回视图效果

一、@RequestParam与HttpServletRequest的区别:
    @RequestParam默认参数必须传，可以不传值，但HttpServletRequest中的参数可以不传
 */
@Controller
@RequestMapping("/hello")
public class HelloMvcContoller {

    /*
    1、用mvn-tomcat插件跑：clean clean tomcat7:run
    http://localhost:8080/sunxj-springmvc1/hello/mvc
    2、部署到idea集成的tomcat上跑： Application context----->"/"
    http://localhost:8080/hello/mvc
     */
    //localhost:8080/hello/mvc
    @RequestMapping("/mvc")
    public String helloMvc() {
        return "hello";
    }



    //localhost:8080/hello/param?id=123
    @RequestMapping("/param")
    public String testParam1(@RequestParam("id")Integer id, Model model) {
        System.out.println("testParam1:"+id);
        model.addAttribute("id");
        return "hello";
    }

    //restful风格
    //localhost:8080/hello/param2/123/sunxj
    @RequestMapping("/param2/{id}/{name}")
    public String testParam2(@PathVariable("id")Integer id,
                             @PathVariable("name")String name,
                             Map<String, Object> model) {
        System.out.println("testParam2:"+name);
        System.out.println("testParam2:"+id);
        model.put("id", id);
        return "hello";
    }


    //restful风格=========>@PathVariable+@RequestParam一起使用
    //localhost:8080/hello/param3/123?name=sunxj
    @RequestMapping("/param3/{id}")
    public String testParam3(@PathVariable("id")Integer id, @RequestParam("name") String name, Map<String, Object> model) {
        System.out.println("testParam3:"+id);
        System.out.println("testParam3:"+name);
        model.put("id", id);
        return "hello";
    }

    //传统的方式：通过HttpServletRequest来获取url参数
    //localhost:8080/hello/param4?id=123&name=sunxj
    @RequestMapping("/param4")
    public String testParam4(HttpServletRequest request) {
        Integer id = Integer.valueOf(request.getParameter("id"));
        String name = request.getParameter("name");
        System.out.println("testParam4:"+id);
        System.out.println("testParam4:"+name);
        return "hello";
    }


    //转发：相对路径
    //重定向：绝对路径
    //localhost:8080/hello/param5?add
    @RequestMapping(value = "/param5",method = RequestMethod.GET, params = "add")
    public String testParam1() {
        System.out.println("testParam5:");
        //绝对路径
//        return "redirect:/hello/param4?id=123&name=sunxj";
        //相对路径
        return "redirect:param4?id=123&name=sunxj";
    }

}
